package xin.yangshuai.zookeeper01.case2;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

public class DistributedLockTest {

    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {

        DistributedLock lock1 = new DistributedLock();

        DistributedLock lock2 = new DistributedLock();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock1.lock();
                    System.out.println(Thread.currentThread().getName() + " 获取到锁");

                    Thread.sleep(5000);

                    lock1.unlock();

                    System.out.println(Thread.currentThread().getName() + " 释放锁");
                } catch (KeeperException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock2.lock();
                    System.out.println(Thread.currentThread().getName() + " 获取到锁");

                    Thread.sleep(5000);

                    lock2.unlock();

                    System.out.println(Thread.currentThread().getName() + " 释放锁");
                } catch (KeeperException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程2").start();
    }
}
